﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Method
namespace backoffice.Data
{
    partial class Method
    {
        public void setMethod(Method m)
        {
            this.MethodID = m.MethodID;
            this.Type = m.Type;
            this.Description = m.Description;
            this.Name = m.Name;
            this.Path = m.Path;
        }
    }
}
#endregion

#region dataMethod
namespace backoffice.Data
{
    public class dataMethod
    {
        #region Create
        public static void createMethod(Method m)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                m.MethodID = Guid.NewGuid();

                db.Methods.InsertOnSubmit(m);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static void createPlant_Method(Plants_Method pm)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                db.Plants_Methods.InsertOnSubmit(pm);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static void createMethod_Material(Methods_Material mm)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                db.Methods_Materials.InsertOnSubmit(mm);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static Method getMethod(Guid guid)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Methods.FirstOrDefault(m => m.MethodID == guid);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Method> getMethods(Expression<Func<Method, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Methods.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Plants_Method> getPlant_Method(Expression<Func<Plants_Method, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Plants_Methods.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Methods_Material> getMethod_Material(Expression<Func<Methods_Material, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Methods_Materials.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changeMethod(Method m)
        {
            try
            {
                if (m == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Method res = db.Methods.FirstOrDefault(me => me.MethodID == m.MethodID);
                    if (res == null) return false;
                    else
                    {
                        res.setMethod(m);
                        db.SubmitChanges();
                        return true;
                    }
                }

            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        #endregion
    }
}
#endregion